package boot.controller;

import boot.entity.User;
import boot.vo.ResponseResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login")
    public ResponseResult login(String username,String password){
        System.out.println("username = " + username + ", password = " + password);
        if("abc".equals(username)&&"123".equals(password)) {
            return ResponseResult.success("登录成功");
        }else{
            return new ResponseResult("500","用户名或密码错误",null);
        }
    }

    @RequestMapping("/regist")
    public ResponseResult regist(User user){
        System.out.println("user = " + user);
        if("abc".equals(user.getUsername())) {
            return new ResponseResult("500","用户名已存在",null);
        }else{
            return ResponseResult.success("注册成功");
        }
    }


    private static Map<Integer,User> users=new HashMap<>();
    private static Integer id=1;

    @PostMapping
    public ResponseResult add(User user){
        System.out.println("UserController.add");
        user.setId(id++);
        users.put(user.getId(),user);
        return ResponseResult.success();
    }

    @DeleteMapping("/{id}")
    public ResponseResult remove(@PathVariable("id") Integer id){
        System.out.println("UserController.remove");
        users.remove(id);
        return ResponseResult.success();
    }

    @PutMapping
    public ResponseResult modify(User user){
        System.out.println("UserController.modify");
        users.put(user.getId(),user);
        return ResponseResult.success();
    }

    @GetMapping("/{id}")
    public ResponseResult findById(@PathVariable("id") Integer id){
        System.out.println("UserController.findById");
        User user=users.get(id);
        return ResponseResult.success(user);
    }

    @GetMapping
    public ResponseResult findAll(){
        System.out.println("UserController.findAll");
        List<User> list=new ArrayList<>(users.values());
        return ResponseResult.success(list);
    }



}
